package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.negocio.entidades.PerfilUsuario;
import com.negocio.entidades.Usuario;


public class AlunoPagesFilter extends AbstractFilter implements Filter {

 @Override
 public void destroy() {

 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  HttpServletRequest req = (HttpServletRequest) request;
  HttpSession session = req.getSession();

  if (session.isNew()) {
	   doLogin(request, response, req);
	   return;
  }

  
  @SuppressWarnings("deprecation")
  Usuario usu = (Usuario) req.getSession(true).getValue("usu");
  try{
  if (!usu.getPerfil().equals(PerfilUsuario.ALUNO)) {
   accessDenied(request, response, req);
   return;
  }

  chain.doFilter(request, response);
  }catch(Exception e){
	  
	  doLogin(request, response, req);
	  return;
  }
  
 }

 @Override
 public void init(FilterConfig arg0) throws ServletException {

 }
}